﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Playables;
/// <summary>
/// clip 移动行为逻辑
/// </summary>
[Serializable]
public class MoveClipBehaviour : PlayableBehaviour
{
     public MoveClip clipData;
     public enum  TweenType
     {
          Linear,
          Deceleration,
          Harmonic,
          Custom,
     }

     public Transform startLocation;
     public Transform endLocation;
     public TweenType tweenType;
     public AnimationCurve customCurve = AnimationCurve.Linear(0, 0, 1, 1);
     
     public Vector3 startingPosition;
     public Quaternion statingRotation = Quaternion.identity;
     private AnimationCurve _lineCurve = AnimationCurve.Linear(0, 0, 1, 1);
     const float _RightAngleInRads = Mathf.PI * 0.5f;
     private AnimationCurve _DecelerationCurve = new AnimationCurve(
          new Keyframe(0, 0, _RightAngleInRads * -1, _RightAngleInRads),
          new Keyframe(1,1,0,0)
     );

     private AnimationCurve _HarmonicCurve = AnimationCurve.EaseInOut(0, 0, 1, 1);

     public bool tweenPosition = true;
     public bool tweenRotation = true;
     public MoveClip data;

     /// <summary>
     /// clip轨道创建时候调用
     /// </summary>
     /// <param name="playable"></param>
     public override void OnPlayableCreate(Playable playable)
     {
          base.OnPlayableCreate(playable);
          Debug.Log("********* OnPlayableCreate**********");
          
     }

     /// <summary>
     /// 在TimeLine开始时候调用
     /// </summary>
     /// <param name="playable"></param>
     public override void OnGraphStart(Playable playable)
     {
          base.OnGraphStart(playable);
          Debug.Log("*********OnGraphStart **********");
          
     }

     /// <summary>
     /// 当clip播放状态设置为播放的时候调用
     /// </summary>
     /// <param name="playable"></param>
     /// <param name="info"></param>
     public override void OnBehaviourPlay(Playable playable, FrameData info)
     {
          base.OnBehaviourPlay(playable, info);
          Debug.Log("*********OnBehaviourPlay **********");
     }

     /// <summary>
     /// 当播放状态设置为暂停时候调用
     /// </summary>
     /// <param name="playable"></param>
     /// <param name="info"></param>
     public override void OnBehaviourPause(Playable playable, FrameData info)
     {
          base.OnBehaviourPause(playable, info);
          Debug.Log("*********OnBehaviourPause **********");
     }

     /// <summary>
     /// clip轨道移除时候调用
     /// </summary>
     /// <param name="playable"></param>
     public override void OnPlayableDestroy(Playable playable)
     {
          base.OnPlayableDestroy(playable);
          Debug.Log("*********OnPlayableDestroy **********");
     }

     /// <summary>
     /// 播放时候 每一帧都会调用
     /// </summary>
     /// <param name="playable"></param>
     /// <param name="info"></param>
     /// <param name="playerData"></param>
     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
     {
          // Debug.Log("********* **********");
          //base.ProcessFrame(playable, info, playerData);
          // if (startLocation)
          // {
          //      startingPosition = startLocation.position;
          //      statingRotation = startLocation.rotation;
          // }
     }

     private bool isCustomEndLocation;

     public void SetEndLocationType(bool customEndLocation)
     {
          this.isCustomEndLocation = isCustomEndLocation;
          Debug.Log("*********SetEndLocationType **********");
     }

     public Vector3 GetEndLocationPositon()
     {
          Vector3 endPosition = endLocation.position;
          if (isCustomEndLocation )
          {
               endPosition = Vector3.one;
          }

          return endPosition;
     }

     public float EvaluteCurrentCuve(float time)
     {
          if (tweenType == TweenType.Custom )
          {
               return 0f;
          }

          switch (tweenType)
          {
               case TweenType.Linear:
                    return _lineCurve.Evaluate(time);
               case TweenType.Deceleration:
                    return _DecelerationCurve.Evaluate(time);
               case TweenType.Harmonic:
                    return _HarmonicCurve.Evaluate(time);
               default:
                    return customCurve.Evaluate(time);
          }
     }
}
